/**
 *
 */
package com.googlecode.bip.dao.deprecated;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import org.apache.log4j.Logger;

import com.googlecode.bip.dao.BipDAO;
import com.googlecode.bip.entity.deprecated.Category;
import cx.ath.choisnet.sql.SimpleQuery;

/**
 *
 * @author Claude CHOISNET
 */
@Deprecated
public class CategoryValueDB extends BipDAO
{
    private static final long serialVersionUID = 1L;
    private final static transient Logger slogger = Logger.getLogger( CategoryValueDB.class );


    /**
     * Create a new CategoryValueDB
     */
    public CategoryValueDB()
    {
    }

//    @Deprecated
//    public CategoryValueDB(
//            final DataSource dataSource
//            )
//    {
////        super( dataSource );
//    }

    /**
     * @param category
     * @param value
     * @return
     * @throws NamingException
     * @throws SQLException
     */
    public String getDescription(
            final Category  category,
            final int       value
            )
        throws SQLException, NamingException
    {
        return getDescription(
                category.getIdcategory(),
                category.getIdlocal(),
                value
                );
    }

    /**
     * @param idcategory
     * @param idLocal
     * @param value
     * @return
     * @throws NamingException
     * @throws SQLException
     */
    public String getDescription(
            final int    idcategory,
            final String idLocal,
            final int    value
            )
        throws SQLException, NamingException
    {
        /*
        CREATE VIEW `bipol`.`categoryvalueview`
        AS
        SELECT C.`idcategory`,
               C.`categorylevel`,
               C.`parentidcategory`,
               C.`order`,
               C.`minvalue`,
               C.`maxvalue`,
               CL.`categoryidlocal`,
               CL.`categoryname`,
               CL.`categorydescription`,
               CVL.`value`,
               CVL.`valuedescription
        */
        String sql = "SELECT `valuedescription` FROM `oldcategoryvalueview` WHERE `idcategory`='"
                        + idcategory
                        + "' AND `idlocal`='"
                        + idLocal
                        + "' AND `value`='"
                        + value
                        + "'";
        final SimpleQuery query = new SimpleQuery( getDataSource() );

        //slogger.info( "getDescription: " + sql );

        try {
            ResultSet   rSet    = query.executeQuery( sql );
            int         count   = 0;
            String      valuedescription = null;

            while( rSet.next() ) {
                valuedescription = rSet.getString( "valuedescription" );
                count++;
                }

            if( count < 2 ) {
                // OK or no value!
                return valuedescription;
                }

            slogger.error(
                "*** getDescription: result for '" + sql
                    + "' : " + count
                    );
            }
        finally {
            query.quietClose();
            }

        // An error is occurred
        return null;
    }
}
